package com.restfinder.user.managedbean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import com.restfinder.managedbean.StandardManagedBean;

import com.restfinder.user.service.AuthenticationService;
import com.restfinder.user.service.UserService;

@ManagedBean(name="loginMB")
@SessionScoped
@Slf4j
public class LoginManagedBean extends StandardManagedBean {

	private static final long serialVersionUID = 1L;
	
	@ManagedProperty(value = "#{authenticationService}")
	@Getter @Setter
	private AuthenticationService authenticationService;	
	@ManagedProperty(name="userService", value = "#{userService}")
	@Getter @Setter
	private UserService userService;	
	@Getter @Setter
	private String login;
	@Getter @Setter
	private String password;
	
	public String login() {
		if (authenticationService.login(login, password)) {
			System.out.println("TAK!!!!!!!!!!!!!!");
			try {
				addObjectToSession("loggedUser", userService.getLoggedUser());
			} catch (Exception e) {
				log.error("Exception", e);
				addWarnMessage("Cannot get logged user", "Cannot get logged user");
				e.printStackTrace();
			}
			return "SUCCESS";
		} else {
			System.out.println("NIE!!!!!!!!!!!!!!");
			addWarnMessage("Exception at authentication", "Exception at authentication");
			return "FAILURE";
		}

	}
	
	public String register(){
		return "SUCCESS";
	}

}
